﻿using System;
using System.Data;
using System.Windows.Forms;

namespace Stote.Forms
{
    public partial class FrmUserAddAndEdit : BaseForm
    {
        Form parentForm = null;
        Dong.BLL.User userBLL = new Dong.BLL.User();
        Dong.BLL.UserType userTypeBLL = new Dong.BLL.UserType();
        Dong.Model.User editModel = null;
        int editId = 0;
        public FrmUserAddAndEdit(int id, Form parent)
        {
            InitializeComponent();
            parentForm = parent;
            Bind();

            btnAdd.Click += BtnAdd_Click;
            btnUserType.Click += BtnUserType_Click;

            if (id == 0)
            {
                editId = 0;
                editModel = null;
                WinText = "添加";
                btnAdd.Text = "添加";
                btnAdd.Image = Properties.Resources.add;
                btnUserType.Visible = true;
            }
            else
            {
                editId = id;
                editModel = userBLL.GetModel(editId);
                WinText = "编辑";
                btnAdd.Text = "保存";
                btnAdd.Image = Properties.Resources.save;
                btnUserType.Visible = false;

                txtUsername.Text = editModel.Account;
                txtPassword.Text = editModel.Password;
                txtPassword.Enabled = false;
                cbUserType.SelectedValue = Convert.ToInt32(editModel.UserTypeId);
                rtbRemark.Text = editModel.Remark;
            }
        }

        private void BtnUserType_Click(object sender, EventArgs e)
        {
            FrmMain mainForm = parentForm.ParentForm as FrmMain;
            mainForm.ToggleSelected("用户类型");
            mainForm.OpenForm(new FrmUserType(), "用户类型");
            this.Close();
        }

        private void Bind()
        {
            DataSet ds = userTypeBLL.GetList(" Status=0");
            DataTable dt = ds.Tables[0];
            DataRow dr = dt.NewRow();
            dr["UserTypeId"] = -1;
            dr["UserTypeName"] = "全部";
            dt.Rows.InsertAt(dr, 0);
            cbUserType.DataSource = dt;
            cbUserType.DisplayMember = "UserTypeName";
            cbUserType.ValueMember = "UserTypeId";
        }

        private void BtnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(txtUsername.Text.Trim()))
                {
                    MessageBox.Show("请输入账号！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                if (string.IsNullOrEmpty(txtPassword.Text.Trim()))
                {
                    MessageBox.Show("请输入密码！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                if (Convert.ToInt32(cbUserType.SelectedValue) == -1)
                {
                    MessageBox.Show("请选择用户类型！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                if (editId == 0)
                {
                    DataSet ds = userBLL.GetList($" Status=0 AND Account='{txtUsername.Text.Trim()}'");
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        MessageBox.Show($"此账号{txtUsername.Text.Trim()}已存在！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }

                    Dong.Model.User model = new Dong.Model.User()
                    {
                        Account = txtUsername.Text.Trim(),
                        Password = txtPassword.Text.Trim(),
                        UserTypeId = Convert.ToInt32(cbUserType.SelectedValue),
                        Remark = rtbRemark.Text.Trim(),
                        CreateUserId = FrmLogin.CurrentUser.UserId,
                    };

                    userBLL.Add(model);

                    // 记录日志：向LogInfo表中插入日志信息
                    // LogContent = "{谁}在{什么时间}修改了{什么}表，列1旧值={是什么}，列1新值={是什么}";
                }
                else
                {
                    DataSet ds = userBLL.GetList($" Status=0 AND Account='{txtUsername.Text.Trim()}' AND UserId!={editModel.UserId}");
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        MessageBox.Show($"修改后的账号{txtUsername.Text.Trim()}已存在！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }

                    editModel.Account = txtUsername.Text.Trim();
                    editModel.UserTypeId = Convert.ToInt32(cbUserType.SelectedValue);
                    editModel.Remark = rtbRemark.Text.Trim();
                    editModel.LastUpdateUserId = FrmLogin.CurrentUser.UserId;
                    editModel.LastUpdateTime = DateTime.Now;
                    userBLL.Update(editModel);
                }
                DialogResult = DialogResult.OK;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
        }
    }
}
